﻿/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Foundation Framework Class Library
 * Version 2.00
 * 
 * ZIP Central Directory Entry Class
 * 		Represents and manages the file header that constitutes an entry in the 
 * central directory section of a PKZIP file.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.IO;

namespace Adaptive.Foundation.IO.Zip
{
    /// <summary>
    /// Represents and manages the file header that constitutes an entry in the 
    /// central directory section of a PKZIP file.
    /// </summary>
    public sealed class ZipCentralDirectoryEntry : ZipInformationHeaderBase, IStorable
    {
        /*--------------------------------------------------------------------------------
         * Private Member Declarations
         *------------------------------------------------------------------------------*/
        #region Private Member Declarations
        /// <summary>
        /// File comment length.
        /// </summary>
        private short _fileCommentLength;
        /// <summary>
        /// disk number start    
        /// </summary>
        private short _diskNumberStart;
        /// <summary>
        /// internal file attributes 
        /// </summary>
        private short _internalAttributes;
        /// <summary>
        /// external file attributes
        /// </summary>
        private int _externalAttributes;
        /// <summary>
        ///  relative offset of local header.
        /// </summary>
        private int _dataLocationInFile;
        /// <summary>
        /// File comment value.
        /// </summary>
        private string _fileComment = string.Empty;
        #endregion

        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the <see cref="ZipCentralDirectoryEntry" /> class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public ZipCentralDirectoryEntry()
            : base()
        {
        }
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources
        /// </summary>
        /// <param name="disposing"><b>true</b> to release both managed and unmanaged resources;
        /// <b>false</b> to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            _fileComment = null;
            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Properties
         *------------------------------------------------------------------------------*/
        #region Public Properties
        /// <summary>
        /// Gets the file comment length.
        /// </summary>
        /// <value>
        /// The length of the file comment entry.
        /// </value>
        public short FileCommentLength
        {
            get
            {
                return _fileCommentLength;
            }
        }
        /// <summary>
        /// Gets or sets the disk number start value.
        /// </summary>
        /// <value>
        /// The number of the diskette on which this file begins.
        /// </value>
        public short DiskNumberStart
        {
            get
            {
                return _diskNumberStart;
            }
            set
            {
                _diskNumberStart = value;
            }
        }
        /// <summary>
        /// Gets or sets the internal file attributes value.
        /// </summary>
        /// <remarks>
        /// Bits 1 and 2 are reserved for use by PKWARE
        /// 
        /// The lowest bit of this field indicates, if set, that
        /// the file is apparently an ASCII or text file.  if not
        /// set, that the file apparently contains binary data.
        ///
        /// The 0x0002 bit of this field indicates, if set, that a 
        /// 4 byte variable record length control field precedes each 
        /// logical record indicating the length of the record. The 
        /// record length control field is stored in little-endian byte
        /// order.  This flag is independent of text control characters, 
        /// and if used in conjunction with text data, includes any 
        /// control characters in the total length of the record. This 
        /// value is provided for mainframe data transfer support.
        /// </remarks>
        public short InternalAttributes
        {
            get
            {
                return _internalAttributes;
            }
            set
            {
                _internalAttributes = value;
            }
        }
        /// <summary>
        /// Gets or sets the external file attributes value.
        /// </summary>
        /// <remarks>
        /// The mapping of the external attributes is host-system dependent.  For
        /// MS-DOS, the low order byte is the MS-DOS directory attribute byte.  if input came 
        /// from standard input, this field is set to zero.
        /// </remarks>
        public int ExternalAttributes
        {
            get
            {
                return _externalAttributes;
            }
            set
            {
                _externalAttributes = value;
            }
        }
        /// <summary>
        /// Gets or sets the relative offset of local header.
        /// </summary>
        /// <remarks>
        /// This is the offset from the start of the first disk on which this file appears, to 
        /// where the local header should be found.  In otherwords, this is the value to provide
        /// to the Seek() method call on the originating file stream.
        /// </remarks>
        public int DataLocationInFile
        {
            get
            {
                return _dataLocationInFile;
            }
            set
            {
                _dataLocationInFile = value;
            }
        }
        /// <summary>
        /// Gets or sets the file comment value.
        /// </summary>
        /// <remarks>
        /// This is the comment for this file.
        /// </remarks>
        public string FileComment
        {
            get
            {
                return _fileComment;
            }
            set
            {
                if (String.IsNullOrEmpty(value))
                {
                    _fileCommentLength = 0;
                    _fileComment = String.Empty;
                }
                else
                {
                    _fileComment = value;
                    _fileCommentLength = Convert.ToInt16(value.Length);
                }
            }
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Protected Methods
         *------------------------------------------------------------------------------*/
        #region Protected Methods
        /// <summary>
        /// Creates the 4 data bytes that make up the signature value used to identify
        /// the header.
        /// </summary>
        /// <returns>The signature value as a 4-byte array.</returns>
        protected override byte[] CreateSignatureDefaultValue()
        {
            return new Byte[] { 80, 75, 1, 2 };
        }
        /// <summary>
        /// Reads any fields not defined on the base class.
        /// </summary>
        /// <param name="reader">An open <see cref="BinaryReader" /> used to read the contents</param>
        /// <remarks>
        /// This allows different child implementations to read (or not read) and interpret
        /// this data as needed.
        /// </remarks>
        protected override void ReadExtraFields(BinaryReader reader)
        {
            _fileCommentLength = reader.ReadInt16();
            _diskNumberStart = reader.ReadInt16();
            _internalAttributes = reader.ReadInt16();
            _externalAttributes = reader.ReadInt32();
            _dataLocationInFile = reader.ReadInt32();
        }
        /// <summary>
        /// Reads any remaining data in the structure.
        /// </summary>
        /// <param name="reader">An open <see cref="BinaryReader" /> used to read the contents</param>
        /// <remarks>
        /// This allows different child implementations to read (or not read) and interpret
        /// this data as needed.
        /// </remarks>
        protected override void ReadRemainingData(BinaryReader reader)
    {
		_fileComment = 
		 System.Text.ASCIIEncoding.ASCII.GetString(reader.ReadBytes(_fileCommentLength));
    }
        /// <summary>
        /// Reads the version-made-by versioning value.
        /// </summary>
        /// <param name="reader">An open <see cref="BinaryReader" /> used to read the contents</param>
        /// <remarks>
        /// This allows different child implementations to read (or not read) and interpret
        /// this data as needed.
        /// </remarks>
        protected override void ReadVersionMadeBy(BinaryReader reader)
        {
            VersionMadeBy.Value = reader.ReadInt16();
        }
        /// <summary>
        /// Writes any fields not defined on the base class.
        /// </summary>
        /// <param name="writer">An open <see cref="BinaryWriter" /> used to writer the contents</param>
        /// <remarks>
        /// This allows different child implementations to write (or not write)
        /// this data as needed.
        /// </remarks>
        protected override void WriteExtraFields(BinaryWriter writer)
        {
            writer.Write(_fileCommentLength);
            writer.Write(_diskNumberStart);
            writer.Write(_internalAttributes);
            writer.Write(_externalAttributes);
            writer.Write(_dataLocationInFile);
        }
        /// <summary>
        /// Writes any remaining data in the structure.
        /// </summary>
        /// <param name="writer">An open <see cref="BinaryWriter" /> used to writer the contents</param>
        /// <remarks>
        /// This allows different child implementations to write (or not write)
        /// this data as needed.
        /// </remarks>
        protected override void WriteRemainingData(BinaryWriter writer)
        {
            writer.Write(System.Text.ASCIIEncoding.ASCII.GetBytes(_fileComment));

        }
        /// <summary>
        /// Writes the made-by versioning data.
        /// </summary>
        /// <param name="writer">An open <see cref="BinaryWriter" /> used to writer the contents</param>
        /// <remarks>
        /// This allows different child implementations to write (or not write)
        /// this data as needed.
        /// </remarks>
        protected override void WriteVersionMadeBy(BinaryWriter writer)
        {
            writer.Write(VersionMadeBy.Value);
        }

        #endregion
    }
}